package org.jeecg.modules.minqing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.minqing.entity.FavorablePolicy;
import org.jeecg.modules.minqing.mapper.FavorablePolicyMapper;
import org.jeecg.modules.minqing.service.IFavorablePolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 优惠政策
 * @Author: jeecg-boot
 * @Date:   2019-10-10
 * @Version: V1.0
 */
@Service
public class FavorablePolicyServiceImpl extends ServiceImpl<FavorablePolicyMapper, FavorablePolicy> implements IFavorablePolicyService {

    @Autowired
    private FavorablePolicyMapper favorablePolicyMapper;

    //建立树形结构
    @Override
    public List<FavorablePolicy> builTree( ){
        List<FavorablePolicy> list =  favorablePolicyMapper.queryList();
        List<FavorablePolicy> treeMenus =new  ArrayList<FavorablePolicy>();
        for(FavorablePolicy menuNode : getRootNode(list)) {
            menuNode=buildChilTree(menuNode,list);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private FavorablePolicy buildChilTree(FavorablePolicy favorablePolicy,List<FavorablePolicy> list){
        List<FavorablePolicy> childlist =new ArrayList<FavorablePolicy>();
        for(FavorablePolicy menuNode : list) {
            if(menuNode.getParentId()!=null && menuNode.getParentId().equals(favorablePolicy.getId())) {
                childlist.add(buildChilTree(menuNode,list));
            }
        }
        favorablePolicy.setChildList(childlist);
        return favorablePolicy;
    }

    //获取根节点
    private List<FavorablePolicy> getRootNode(List<FavorablePolicy> list) {
        List<FavorablePolicy> rootMenuLists =new  ArrayList<FavorablePolicy>();
        for(FavorablePolicy favorablePolicy : list) {
            if("0".equals(favorablePolicy.getParentId())) {
                rootMenuLists.add(favorablePolicy);
            }
        }
        return rootMenuLists;
    }
    @Override
    public List<FavorablePolicy> queryListByIds(List<String> ids ){
        return favorablePolicyMapper.queryListByIds(ids);
    }
}
